//
//  MineVC.swift
//  RPay
//
//  Created by Charlie Liao on 2017/6/27.
//  Copyright © 2017年 Charlie. All rights reserved.
//

import UIKit
import SnapKit

class MineVC: BaseVC, UITableViewDataSource, UITableViewDelegate {
    
    // MARK: - Life Cycle
    override func loadView() {
        
        view = tableView
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "CellID")
        
        tableView.tableFooterView!.addSubview(logoutBtn)
        logoutBtn.snp.makeConstraints { (maker) in
            maker.left.right.equalToSuperview()
            maker.height.equalTo(46)
            maker.centerY.equalToSuperview()
        }
        
        logoutBtn.addTarget(self, action: #selector(MineVC.tapLogout), for: .touchUpInside)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = LocalStr("我的")
        // Do any additional setup after loading the view.
    }
    
    // MARK: - table View data source
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 1
            
        } else if section == 1 {
            return list.count
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 0 {
            var cell = tableView.dequeueReusableCell(withIdentifier: "CellID1")
            if cell == nil {
                cell = UITableViewCell(style: .value1, reuseIdentifier: "CellID1")
                cell!.textLabel!.text = LocalStr("账号")
                cell?.selectionStyle = .none
            }
            cell!.detailTextLabel!.text = UserShare.shared.loginUser?.userName
            
            return cell!
            
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath)
            cell.accessoryType = .disclosureIndicator
            cell.textLabel!.text = list[indexPath.row]
            return cell
        }
    }
    
    
    // MARK: - Table View delegate
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        if indexPath.section == 1 {
            let title = list[indexPath.row]
            if title == LocalStr("RH账户") {
                let vc = AccountVC()
                vc.hidesBottomBarWhenPushed = true
                navigationController!.pushViewController(vc, animated: true)
            }
            else if title == LocalStr("我的订单") {
                let vc = MyOrderVC()
                vc.hidesBottomBarWhenPushed = true
                navigationController!.pushViewController(vc, animated: true)
            }
            else if title == LocalStr("收付记录") {
                let vc = TradingRecordVC()
                vc.hidesBottomBarWhenPushed = true
                navigationController!.pushViewController(vc, animated: true)
            }
        }
    }
    
    // MARK: - Event respond
    /// 点击退出登录
    func tapLogout() -> Void {
        IFShared().pushLoginVC()
        UserShare.shared.loginUser = nil
    }
    
    
    // MARK: - Get / Set
    
    let list = [LocalStr("RH账户"), LocalStr("我的订单"), LocalStr("收付记录")]
    
    var tableView: UITableView = {
        let tableView = UITableView(frame: CGRect.zero, style: .grouped)
        tableView.rowHeight = 44
        tableView.sectionHeaderHeight = 15
        tableView.sectionFooterHeight = 0
        tableView.estimatedSectionHeaderHeight = 15
        tableView.estimatedSectionFooterHeight = 0
        tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 76))
        
        tableView.backgroundColor = UIColor.appBackground
        return tableView
    }()
    
    var logoutBtn: UIButton = {
        let logoutBtn = UIButton(type:.custom)
        logoutBtn.setTitle(LocalStr("退出登录"), for: .normal)
        logoutBtn.titleLabel!.font = UIFont.appRegularFont(ofPX: 16)
        logoutBtn.setTitleColor(UIColor.appTheme, for: .normal)
        logoutBtn.setBackgroundImage(UIImage(graphics: UIColor.white), for: .normal)
        logoutBtn.setBackgroundImage(UIImage(graphics: UIColor.gray.withAlphaComponent(0.35)), for: .highlighted)
        return logoutBtn
    }()
    
}
